package com.ffcs.web.utils;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * Created with e-leaflets.
 * User: 蔡协鑫
 * Date: 2014/7/7
 * Time: 9:54
 */

public class MailUtil {
    public static void sendHtmlMail(String subject,String []addrs,String contentHtml,JavaMailSenderImpl mailSender) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        //设置发送人名片
        helper.setFrom(mailSender.getUsername());
        //设置收件人名片和地址
        helper.setTo(addrs);
        // 邮件发送时间
        helper.setSentDate(new Date());
        //主题
        helper.setSubject(subject);
        // 邮件内容，注意加参数true，表示启用html格式
        helper.setText(contentHtml,true);
        //发送
        mailSender.send(mimeMessage);
    }
}
